package com.sa07.ui.components;

import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import com.sa07.ui.model.DefaultTestTableModel;
import com.sa07.ui.model.TestTableCellRenderer;

/**
 * 默认测试表格
 * 
 * @author Long.shangbo
 * 
 * 2014-2-17 上午1:01:00
 */
public class JtestJTable extends JTable {

	private static final long serialVersionUID = -2204734217361128857L;

	public JtestJTable() {

	}

	public JtestJTable(TableModel tableModel) {
		super(tableModel);
		setAutoscrolls(true);
		setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		setShowGrid(true);
		int rowHeight = 22;
		//设置高度
		setRowHeight(rowHeight);
		setFont(new Font(null, Font.PLAIN, 13));
		
		int columnHeight = 25;
		JTableHeader header = getTableHeader();
		Dimension currDim =  header.getPreferredSize();
		header.setPreferredSize(new Dimension(currDim.width, columnHeight));
		header.setReorderingAllowed(false);   
		header.setFont(new Font(null, Font.BOLD, 12));
		
		//设置列宽
		TableColumnModel columnModel = header.getColumnModel();
		TableColumn tableColumn = null;
		String tableColumnText = null;
		
		for(int i = 0, columnCount = columnModel.getColumnCount(); i < columnCount; i++){
			tableColumn = columnModel.getColumn(i);
			tableColumnText = tableColumn.getHeaderValue().toString();
			tableColumn.setResizable(false);
			int columnWidth = 90;
			
			if(tableColumnText.equals(DefaultTestTableModel.TEST_RESULT)){
				columnWidth = 70;
				tableColumn.setPreferredWidth(columnWidth);
				tableColumn.setMaxWidth(columnWidth);
			} else if(tableColumnText.equals(DefaultTestTableModel.TEST_ID)){
				columnWidth = 55;
				tableColumn.setPreferredWidth(columnWidth);
				tableColumn.setMaxWidth(columnWidth);
			} else if(tableColumnText.equals(DefaultTestTableModel.TEST_PRODUCT_CODE)){
				columnWidth = 160;
				tableColumn.setPreferredWidth(columnWidth);
				tableColumn.setMaxWidth(columnWidth);
			} else if(tableColumnText.equals(DefaultTestTableModel.TEST_TIME)){
				tableColumn.setResizable(true);
			} else if(tableColumnText.equals(DefaultTestTableModel.TEST_ITEM_NAME)){
				tableColumn.setResizable(true);
			}
		}
	}
	
	@Override
	public TableCellRenderer getCellRenderer(int row, int column) {
		return new TestTableCellRenderer();
	}
}
